package com.ruoyi.bizsys.service.impl;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.bizsys.domain.CustContactsRelative;
import com.ruoyi.bizsys.domain.DeviceContacts;
import com.ruoyi.bizsys.mapper.CustContactsRelativeMapper;
import com.ruoyi.bizsys.service.ICustContactsRelativeService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
    import com.ruoyi.bizsys.mapper.AppDeviceContactapplistRelativeMapper;
import com.ruoyi.bizsys.domain.AppDeviceContactapplistRelative;
import com.ruoyi.bizsys.service.IAppDeviceContactapplistRelativeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 设备app通讯录关联Service业务层处理
 *
 * @author african-loan
 * @date 2022-03-25
 */
@Service
public class AppDeviceContactapplistRelativeServiceImpl extends ServiceImpl<AppDeviceContactapplistRelativeMapper,AppDeviceContactapplistRelative> implements IAppDeviceContactapplistRelativeService {


    @Autowired
    private CustContactsRelativeMapper custContactsRelativeMapper;

    /**
     * 查询设备app通讯录关联
     *
     * @param id 设备app通讯录关联ID
     * @return 设备app通讯录关联
     */
    @Override
    public AppDeviceContactapplistRelative selectAppDeviceContactapplistRelativeById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询设备app通讯录关联列表
     *
     * @param appDeviceContactapplistRelative 设备app通讯录关联
     * @return 设备app通讯录关联
     */
    @Override
    public List<AppDeviceContactapplistRelative> selectAppDeviceContactapplistRelativeList(AppDeviceContactapplistRelative appDeviceContactapplistRelative) {
        return this.list(new QueryWrapper<>(appDeviceContactapplistRelative).last("order by id desc"));
    }

    /**
     * 新增设备app通讯录关联
     *
     * @param appDeviceContactapplistRelative 设备app通讯录关联
     * @return 结果
     */
            @Override
    public int insertAppDeviceContactapplistRelative(AppDeviceContactapplistRelative appDeviceContactapplistRelative) {
                                                                                                                                                                                                    appDeviceContactapplistRelative.setCreateTime(DateUtils.getNowDate());
                            return baseMapper.insert(appDeviceContactapplistRelative);
    }

    /**
     * 修改设备app通讯录关联
     *
     * @param appDeviceContactapplistRelative 设备app通讯录关联
     * @return 结果
     */
            @Override
    public int updateAppDeviceContactapplistRelative(AppDeviceContactapplistRelative appDeviceContactapplistRelative) {
                                                                                                                                                                                                    return baseMapper.updateById(appDeviceContactapplistRelative);
    }

    /**
     * 批量删除设备app通讯录关联
     *
     * @param ids 需要删除的设备app通讯录关联ID
     * @return 结果
     */
            @Override
    public int deleteAppDeviceContactapplistRelativeByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除设备app通讯录关联信息
     *
     * @param id 设备app通讯录关联ID
     * @return 结果
     */
    @Override
    public int deleteAppDeviceContactapplistRelativeById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public String getBy7DdayData(String fingerprint, String userId, int type) {
        return baseMapper.getBy7DdayData(fingerprint,userId,type);
    }

    @Override
    public int checkContactAppList(String fingerprint,String userId) {
        boolean concats = StringUtils.isNotBlank(baseMapper.getBy7DdayData(fingerprint, userId, 1));
        boolean appList = StringUtils.isNotBlank(baseMapper.getBy7DdayData(fingerprint, userId, 2));

        if(concats){
            if(appList){
                //全部不需要上传
                return 4;
            }else{
                //只需要上传app
                return 2;
            }
        }else{
            if(appList){
                //只需要上传通讯录
                return 1;
            }else{
                //全部都要上传
                return 3;
            }
        }
    }

    @Override
    public void copyData(String fingerprint, String userId) {
        QueryWrapper<CustContactsRelative> applyContactsQueryWrapper = new QueryWrapper<>();
        applyContactsQueryWrapper.eq("cust_no", userId).last("limit 1");
        CustContactsRelative one = custContactsRelativeMapper.selectOne(applyContactsQueryWrapper);
        if (one == null || StringUtils.isBlank(one.getContactsTableName())) {
            baseMapper.copyData(fingerprint,userId);
        }

    }

}
